<?php 
//水印与缩略图类
//水印,就是把指定的水印复制到目标上，并加上透明效果
//缩略图，就是把大图复制到小尺寸画布上
defined('ACC')||exit('ACC Denied');
class ImageTool{
	protected $code='';
	/**
	 * @param $image
	 * @return bool
	 */
	public static function imageInfo($image){
		if (!file_exists($image)) {
			return false;
		}
		$info=getimagesize($image);
		if ($info == false) {
			return false;
		}
		$img['width']=$info[0];
		$img['height']=$info[1];
		$img['ext']=substr($info['mime'],strpos($info['mime'], '/')+1);
		return $img;
	}
	/**
	 * @param $dst
	 * @param $water
	 * @param null $save
	 * @param int $alpha
	 * @param int $pos
	 * @return bool
	 */
	public static function water($dst,$water,$save=null,$alpha=50,$pos=2){
		//先保证两个图片存在
		if (!file_exists($dst)||!file_exists($water)) {
			return false;
		}
		//首先保证水印不能比待操作图片还大
		$dinfo=self::imageInfo($dst);
		$winfo=self::imageInfo($water);
		if ($winfo['height']>$dinfo['height']||$winfo['width']>$dinfo['width']){
			return false;
		}
		//两张图，读到画布上，图片可能是png可能是jpeg，用什么函数读？
		$dfunc='imagecreatefrom'.$dinfo['ext'];
		$winfo='imagecreatefrom'.$winfo['ext'];
		if (!function_exists($dfunc)||!function_exists($wfunc)) {
			return false;
		}
		//动态加载函数来创建画布
		$dim=$dfunc($dst);//创建待操作的画布
		$wim=$wfunc($water);//创建水印画布
		//根据谁引得位置，计算粘贴的坐标
		switch ($pos) {
			case '0'://左上角
				$posx=0;
				$posy=0;
				break;
			case '1'://右上角
				$posx=$dinfo['width']-$winfo['width'];
				$posy=0;
				break;
			case '3'://左下角
				$posx=0;
				$posy=$dinfo['height']-$winfo['height'];
				break;
			default:
				$posx=$dinfo['width']-$winfo['width'];
				$posy=$dinfo['height']-$winfo['height'];
		}
		//加水印
		imagecopymerge($dim, $wim, $posx,$posy,0,0,$winfo['width'],$winfo['height'],$alpha);
		//	保存
		if (!$save) {
			$save=$dst;
			unlink($dst);//删除原图
		}
		$createfunc='image'.$dinfo['ext'];
		$createfunc($dim,$save);
		imagedestroy($dim);
		imagedestroy($wim);
		return true;
	}
	/**
	 * @param $dst
	 * @param null $save
	 * @param int $width
	 * @param int $height
	 * @return bool
	 * 	生成缩略图
	 *  等比例缩放,两边留白
	 */
	public static function thumb($dst,$save=null,$width=200,$height=200){
		//首先判断待处理的图片存不存在
		$dinfo=self::imageInfo($dst);
		if ($dinfo == false) {
			return false;
		}
		//计算缩放比例
		$calc=min($width/$dinfo['width'],$height/$dinfo['height']);
		//创建原始画布
		$dfunc='imagecreatefrom'.$dinfo['ext'];
		$dim=$dfunc($dst);
		//创建缩略画布
		$tim=imagecreatetruecolor($width, $height);
		//创建白色填充画布
		$white=imagecolorallocate($tim,255,255,255);
		//填充缩略画布
		imagefill($tim,0,0,$white);
		//复制并缩略
		$dwidth=(int)$dinfo['width']*$calc;
		$dheight=(int)$dinfo['height']*$calc;
		$paddingx=(int)($width-$dwidth)/2;
		$paddingy=(int)($height-$dheight)/2;
		imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidth,$dheight,$dinfo['width'],$dinfo['height']);
		//保存图片	
		if (!$save) {
			$save=$dst;
			unlink($dst);
		}
		$createfunc='image'.$dinfo['ext'];
		$createfunc($tim,$save);
		imagedestroy($dim);
		imagedestroy($tim);
		return true;
	}
	/**
	 * @param int $width
	 * @param int $height
	 *	验证码类
	 */
	public static function captcha($width=50,$height=50){
		//造画布
		$image=imagecreatetruecolor($width, $height);
		//造背景色
		$gray=imagecolorallocate($image,200,200,200);
		//填充背景
		imagefill($image,0,0,$gray);
		//造随机字体颜色
		$color=imagecolorallocate($image,mt_rand(0,125), mt_rand(0,125), mt_rand(0,125));
		//造随机线条颜色
		$color1=imagecolorallocate($image, mt_rand(100,125), mt_rand(100,125), mt_rand(100,125));
		$color2=imagecolorallocate($image, mt_rand(100,125), mt_rand(100,125), mt_rand(100,125));
		$color3=imagecolorallocate($image, mt_rand(100,125), mt_rand(100,125), mt_rand(100,125));
		//在画布上画线
		imageline($image,mt_rand(0,50), mt_rand(0,50), mt_rand(0,50), mt_rand(0,50), $color1);
		imageline($image,mt_rand(0,50), mt_rand(0,50), mt_rand(0,50), mt_rand(0,50), $color2);
		imageline($image,mt_rand(0,50), mt_rand(0,50), mt_rand(0,50), mt_rand(0,50), $color3);
		//在画布上写字
		$text=substr(str_shuffle(ABCDEFGHJKOMNaqwertyoikm98765432),0,4);
		// $this->code.=$text;
		imagestring($image,5,7,5,$text,$color);
		//显示，销毁
		ob_clean();
		header('content-type:image/jpeg');
		imagejpeg($image);
		imagedestroy($image);
	}
	/**
	 * @return string
	 */
	public function getCode() {
    	return strtolower($this->code);
 }
} 

